package com.cloud.logger.zchain.zchainserver.controller;

import com.cloud.logger.zchain.core.common.base.ResultMessage;
import com.cloud.logger.zchain.core.common.constant.ResultCodeConstant;
import com.cloud.logger.zchain.zchainserver.entity.ZchainOrgnation;
import com.cloud.logger.zchain.zchainserver.entity.ZchainUser;
import com.cloud.logger.zchain.zchainserver.mongo.ZchianOrgnationRepository;
import com.cloud.logger.zchain.zchainserver.mongo.ZchianUserRepository;
import com.cloud.logger.zchain.zchainserver.service.ZchainOrgnationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: wcy
 * Date: 2017/11/7
 * Time: 13:31
 */
@RestController
@RequestMapping(value = "user")
@Api(value = "/user", description = "用户管理接口 -by wcy")
public class UserManageCtl {

    @Resource
    private ZchianOrgnationRepository orgnationRepository;

    @Resource
    private ZchianUserRepository userRepository;

    @Resource
    private ZchainOrgnationService orgnationService;

    @PostMapping(value = "saveOrg")
    @ApiOperation(value = "保存机构信息 -by wcy")
    public ResultMessage saveOrg(@RequestBody ZchainOrgnation orgnation){
        ResultMessage resultMessage = new ResultMessage();

        orgnation = orgnationRepository.save(orgnation);

        if (orgnation.getId() !=null){
            resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
            resultMessage.setId(orgnation.getId()+"");
        }else{
            resultMessage.setCode(ResultCodeConstant.SERVICE_ERROR_CODE);
        }
        return resultMessage;
    }

    @PostMapping(value = "saveUser")
    @ApiOperation(value = "保存用户信息")
    public ResultMessage saveUser(@RequestBody ZchainUser user){
        ResultMessage resultMessage = new ResultMessage();

        user = userRepository.save(user);

        if (user.getId() !=null){
            resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
            resultMessage.setId(user.getId()+"");
        }else{
            resultMessage.setCode(ResultCodeConstant.SERVICE_ERROR_CODE);
        }
        return resultMessage;
    }

    @PutMapping(value = "updateUser")
    @ApiOperation(value = "更新用户信息")
    public ResultMessage updateUser(@RequestBody ZchainUser user){
        ResultMessage resultMessage = new ResultMessage();
        user = userRepository.save(user);
        if (user.getId() !=null){
            resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
            resultMessage.setId(user.getId()+"");
        }else{
            resultMessage.setCode(ResultCodeConstant.SERVICE_ERROR_CODE);
        }
        return resultMessage;
    }

    @PutMapping(value = "updateOrg")
    @ApiOperation(value = "更新机构信息")
    public ResultMessage updateOrg(@RequestBody ZchainOrgnation orgnation){
        ResultMessage resultMessage = new ResultMessage();
        orgnation = orgnationRepository.save(orgnation);
        if (orgnation.getId() !=null){
            resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
            resultMessage.setId(orgnation.getId()+"");
        }else{
            resultMessage.setCode(ResultCodeConstant.SERVICE_ERROR_CODE);
        }
        return resultMessage;
    }

    @GetMapping(value = "{id}")
    @ApiOperation(value = "通过id获取用户信息")
    public ResultMessage<ZchainUser> getUser(@PathVariable String id){
        ResultMessage resultMessage = new ResultMessage();
        ZchainUser resultinfo = userRepository.findOne(id);

        if (resultinfo !=null){
            resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
            resultMessage.setContent(resultinfo);
        }else{
            resultMessage.setCode(ResultCodeConstant.SERVICE_ERROR_CODE);
        }
        return resultMessage;
    }

    @GetMapping(value = "org/{id}")
    @ApiOperation(value = "通过id获取机构信息")
    public ResultMessage<ZchainOrgnation> getOrg(@PathVariable String id){
        ResultMessage resultMessage = new ResultMessage();
        ZchainOrgnation resultinfo = orgnationRepository.findOne(id);

        if (resultinfo !=null){
            resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
            resultMessage.setContent(resultinfo);
        }else{
            resultMessage.setCode(ResultCodeConstant.SERVICE_ERROR_CODE);
        }
        return resultMessage;
    }

    @ApiOperation(value = "通过id删除用户信息")
    @DeleteMapping(value = "/{id}")
    public ResultMessage remove(@PathVariable("id") String id) {
        ResultMessage resultMessage = new ResultMessage();
        userRepository.delete(id);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        return  resultMessage;
    }

    @ApiOperation(value = "通过id删除机构信息")
    @DeleteMapping(value = "org/{id}")
    public ResultMessage removeOrg(@PathVariable("id") String id) {
        ResultMessage resultMessage = new ResultMessage();
        orgnationRepository.delete(id);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        return  resultMessage;
    }

    @ApiOperation(value = "获取的机构导航栏列表")
    @GetMapping(value = "org/find")
    public ResultMessage findOrgList() {
        ResultMessage resultMessage = new ResultMessage();
        List<ZchainOrgnation> orgnations = orgnationService.getZchainOrgnation("-1");
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        resultMessage.setContent(orgnations);
        return  resultMessage;
    }

    @ApiOperation(value = "获取的机构导航栏列表")
    @GetMapping(value = "findUsersByOrgId/{id}")
    public ResultMessage findUsersByOrgId(@PathVariable String id) {
        ResultMessage resultMessage = new ResultMessage();
        List<ZchainUser> users = userRepository.findByOrgId(id);
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        resultMessage.setContent(users);
        return  resultMessage;
    }

    @ApiOperation(value = "获取机构和人员的树状结构数据 by-zhengyt")
    @GetMapping(value = "findOrgAndUsers")
    public ResultMessage findOrgAndUsers() {
        return orgnationService.findOrgAndUsers();
    }

}